class AlbumsController < ApplicationController
  
  include AuthenticatedSystem
  before_filter :login_required
  

  #---------------------------------------------------------------------------------------------------------
  # INDEX
  # GET /albums
  # GET /albums.xml
  #---------------------------------------------------------------------------------------------------------
  def index
    
    #-----------------------------------
    # Variables
    #-----------------------------------
    @albums = Album.find(:all)
    
    #-----------------------------------
    # Albums à afficher
    #-----------------------------------
    # Si l'utilisateur est l'administrateur
    if isAdmin?()
      # On affiche tous les albums
      @albums_utilisateur = Album.find(:all)
      @isAdmin = true
    else
      @isAdmin = false
      # On affiche ses albums
      @albums_utilisateur = []
      i=0
      # Pour tous les albums
      for album in @albums
        # L'album n'appartient pas à l'utilisateur connecté
        album_utilisateur = false
        # Pour chaque images de l'album
        for image in album.picture
          # Si l'image appartient à l'utilisateur
          if image.user_id == current_user.id
            album_utilisateur = true
          end
        end
        # Si l'album appartient à l'utilisateur
        if album_utilisateur
          @albums_utilisateur[i] = album
          i=i+1
        end
      end
   end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @albums }
    end
  end

  #---------------------------------------------------------------------------------------------------------
  # SHOW
  # GET /albums/1
  # GET /albums/1.xml
  #---------------------------------------------------------------------------------------------------------
  def show
    #-----------------------------------
    # Variables
    #-----------------------------------
    @album = Album.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @album }
    end
  end

  #---------------------------------------------------------------------------------------------------------
  # NEW
  # GET /albums/new
  # GET /albums/new.xml
  #---------------------------------------------------------------------------------------------------------
  def new

    #-----------------------------------
    # Variables
    #-----------------------------------
    @album = Album.new    
    @liste_continent = Continent.find :all, :order=>"name asc"
    @liste_pays = Country.find :all, :order=>"name asc"
    # Si l'utilisateur est l'administrateur
    if isAdmin?()
      @liste_images = Picture.find :all
    else
      @liste_images = Picture.find(:all, :conditions => ["user_id = :id",{:id => current_user.id}])
    end

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @album }
    end
  end
  
  #---------------------------------------------------------------------------------------------------------
  # UPDATELIST - met à jour la liste des pays en fonction du continent choisi
  #---------------------------------------------------------------------------------------------------------
  def updatelist
    choix_continent = params['id_continent']
    @liste_pays = Country.find_all_by_continent_id(choix_continent, :order => "name desc")
  end

  
  #---------------------------------------------------------------------------------------------------------
  # EDIT
  # GET /albums/1/edit
  #---------------------------------------------------------------------------------------------------------
  def edit

    #-----------------------------------
    # Variables
    #-----------------------------------
    @album = Album.find(params[:id])
    # Si l'utilisateur est l'administrateur
    if isAdmin?()
      @liste_images = Picture.find :all
    else
      @liste_images = Picture.find(:all, :conditions => ["user_id = :id",{:id => current_user.id}])
    end
    # Pays actuel
    @country = Country.find(:all, :order => 'name', :conditions => ["continent_id = :id",{:id => @album.country.continent_id}]) 
    
    #-----------------------------------
    # Construit la liste des continents
    # avec comme premier contient celui
    # de l'album 
    #-----------------------------------
    autre_continents = Continent.find(:all, :order => 'name', :conditions => ["id != :id",{:id => @album.country.continent_id}])
    continent_affecte = @album.country.continent
    @liste_continent = [continent_affecte]
    i=1
    autre_continents.each do |continent|
      @liste_continent[i]=continent
      i=i+1
    end
    
    #-----------------------------------
    # Donne la liste de association pour
    # cet album
    #-----------------------------------
    associations = Association.find(:all)
    @associations_un_album = []
    i=0
    for association in associations
      if association.album_id == @album.id
        @associations_un_album[i] = association
        i=i+1
      end
    end

  end

  
  #---------------------------------------------------------------------------------------------------------
  # CREATE
  # POST /albums
  # POST /albums.xml
  #---------------------------------------------------------------------------------------------------------
  def create
    
    #-----------------------------------
    # Variables
    #-----------------------------------
    @album = Album.new(params[:album])
    images = Picture.find(:all)
    @associations_de_album = params[:checkbox]

    respond_to do |format|
      if @album.save
        flash[:notice] = 'Album was successfully created.'
        format.html { redirect_to(@album) }
        format.xml  { render :xml => @album, :status => :created, :location => @album }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @album.errors, :status => :unprocessable_entity }
      end
    end
    
    #-----------------------------------
    # Ajoute les nouvelles associations
    # de cet album
    #-----------------------------------
    # Pour chaque image
    for image in images
      # Si l'image a été cochée
      if @associations_de_album["#{image.id}"] == '1'
        assoc = Association.new(:picture_id => image.id, :album_id => @album.id)
        assoc.save
      end
    end
    
  end

  
  #---------------------------------------------------------------------------------------------------------
  # UPDATE
  # PUT /albums/1
  # PUT /albums/1.xml
  #---------------------------------------------------------------------------------------------------------
  def update
    @album = Album.find(params[:id])
    
    #-----------------------------------
    # Variables
    #-----------------------------------
    associations = Association.find(:all)
    images = Picture.find(:all)
    @associations_de_album = params[:checkbox]
    
    #-----------------------------------
    # Efface toutes les associations de
    # cet album
    #-----------------------------------
    # Pour toutes les associations
    for une_association in associations
      # Si l'image appartient deja à l'album
      if une_association.album_id == @album.id
        # Suppression de l'association
        une_association.delete
      end
    end
 
    #-----------------------------------
    # Ajoute les nouvelles associations
    # de cet album
    #-----------------------------------
    # Pour chaque image
    for image in images
      # Si l'image a été cochée
      if @associations_de_album["#{image.id}"] == '1'
        assoc = Association.new(:picture_id => image.id, :album_id => params[:id])
        assoc.save
      end
    end 
    
    respond_to do |format|
      if @album.update_attributes(params[:album])
        flash[:notice] = 'Album was successfully updated.'
        format.html { redirect_to(@album) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @album.errors, :status => :unprocessable_entity }
      end
    end
  end

  
  #---------------------------------------------------------------------------------------------------------
  # DESTROY
  # DELETE /albums/1
  # DELETE /albums/1.xml
  #---------------------------------------------------------------------------------------------------------
  def destroy

    #-----------------------------------
    # Variables
    #-----------------------------------
    @album = Album.find(params[:id])
    associations = Association.find(:all)

    #-----------------------------------
    # Efface toutes les associations de
    # cet album
    #-----------------------------------
    # Pour toutes les associations
    for une_association in associations
      # Si l'image appartient deja à l'album
      if une_association.album_id == @album.id
        # Suppression de l'association
        une_association.destroy
      end
    end

    # On supprimer l'album
    @album.destroy

    respond_to do |format|
      format.html { redirect_to(albums_url) }
      format.xml  { head :ok }
    end
  end
end
